﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using blackhouse.BlackApart.Business.Apartments;
using blackhouse.BlackApart.Web;
using blackhouse.BlackApart.Business.Opinions;
using blackhouse.BlackApart.Business;
using blackhouse.BlackApart.Business.Prices;
using System.Globalization;
//using mojoPortal.Web.Framework;
using blackhouse.JavaScript.JQuery;
using blackhouse.BlackApart.Web.Navigation;
using blackhouse.BlackApart.Business.Reservations;
using blackhouse.mojoPortalExtend;
using blackhouse.BlackApart.Business.ApartmentServices;
using blackhouse.StareKino.Web.ApartmentType;
using blackhouse.WebNemo.Web.Controls;
using blackhouse.BlackApart.WebNemo;
using blackhouse.WebNemo.Business.Groups;

namespace blackhouse.StareKino.Web.Modules {
    public partial class ApartmentGroupLine : System.Web.UI.UserControl {

        #region Fields

        private IApartmentGroup group;
        private NemoApartmentType nemoGroup;
        private AdditionalInformationApartmentType additionalInfo;
        private CinemaActors actors;
        private Factory factory;
        private SearchState queryState = SearchState.GetFromContext();

        #endregion

        #region Properties

        private Factory Factory {
            get {
                if (this.factory == null)
                    this.factory = Factory.Instance();
                return this.factory;
            }
        }

        public SearchState QueryState {
            get {
                return this.queryState;
            }
            set {
                this.queryState = value;
            }
        }

        #endregion

        #region Event Handlers

        protected override void OnLoad(EventArgs e) {
            base.OnLoad(e);

            //this.AddJs();
        }

        #endregion

        #region Methods

        //private void AddJs() {

        //    ClientScriptManager jsManager = Page.ClientScript;
        //    string scriptName = "prettyPhoto";
        //    string scriptSrc = "/prettyphoto/js/jquery.prettyPhoto.js";
        //    if (!jsManager.IsClientScriptIncludeRegistered(scriptName)) {
        //        jsManager.RegisterClientScriptInclude(scriptName, scriptSrc);
        //    }

        //    JQueryLibraries jql = new JQueryLibraries();
        //    jql.IncludeJQueryTipsy();
        //    jql.IncludeFileBasedOnJQuery("blackhouse.Aparts.Web.Scripts.displayApartmentLine.js,blackhouse.Aparts.Web");
        //}

        public void PopulateControl(IApartmentGroup group) {
            this.PopulateControl(group, null);
        }

        public void PopulateControl(IApartmentGroup group, Variant variant) {
            this.SetInstance(group, variant);

            this.imgMainPhoto.ImageUrl = StareKinoHelper.GetApartmentPhotoUrl(group.MainPhoto, new System.Drawing.Size(480, 480));
            this.imgMainPhoto.AlternateText = group.Name + " - " + group.MainPhoto.Category + " - " + group.MainPhoto.Description;

            this.ltlApartmentName.Text = this.group.ShortName;
            this.lblDescription.Text = this.group.LidDescription;

            double s = Math.Ceiling(this.group.Stars * 100 / 5);
            if (s > 100) s = 100;
            if (s < 0) s = 0;
            this.pnlStars.Width = Unit.Percentage(s);

            this.ltlActors.Text = this.actors.ToString();
            //this.lnkToPopupMap.Attributes.Add("lat", this.group.DefaultLocalization.Localization.Latitude.ToString(CultureInfo.GetCultureInfo("en-US")));
            //this.lnkToPopupMap.Attributes.Add("long", this.group.DefaultLocalization.Localization.Longitude.ToString(CultureInfo.GetCultureInfo("en-US")));
            //this.lnkToPopupMap.NavigateUrl = "/Pages/ShowApartmentMap.aspx?groupId=" + this.group.Id + "&iframe=true&width=960&height=550";
            //this.lnkToPopupMap.ToolTip = "Lokalizacja apartamentu - " + this.group.ShortName;

            //IApartmentPhoto photo = this.Factory.GetApartmentPhoto().GetApartmentTypePhotos(this.group.Id).FirstOrDefault(ap => ap.Category.Equals("schemat", StringComparison.CurrentCultureIgnoreCase));
            //if (photo != null) {
            //    this.lnkToPopupScheme.NavigateUrl = UrlUtils.ApartmentPhotoBigImageUrl(photo.PhotoName);
            //    this.lnkToPopupScheme.ToolTip = group.Name + " - " + photo.Category + " - " + photo.Description;
            //}
            //else {
            //    this.lnkToPopupScheme.Visible = false;
            //    this.lnkToPopupMap.CssClass = this.lnkToPopupMap.CssClass.ReplaceEx("-1", "-2");
            //}

            this.lblDescription.Text = this.group.LidDescription;
            this.lnkFromDescription.ToolTip = "Opis " + this.group.Name;
            string apartmentUrl = PageProvider.AdjustUrlToCurrentSite(this.group.Url);
            if (this.QueryState.IsSet)
                apartmentUrl = apartmentUrl + "?search=" + this.QueryState.ToString();
            this.lnkFromDescription.NavigateUrl = apartmentUrl;

            if (this.QueryState.IsSet) {
                this.lnkBook.Visible = true;
                this.lnkBook.NavigateUrl = NavigationProvider.Instance().GetUrl(ReservationPage.GetPreInformationPage(this.group.Id));
                this.lnkBook.ToolTip = GetGlobalResourceObject("Reservation", "makeReservation2") + " - " + this.group.Name;
                this.lnkBook.Attributes.Add("rel", "nofollow");
            } else {
                this.lnkCheck.Visible = true;
            }

            if (!String.IsNullOrEmpty(this.additionalInfo.ApartmentGroupName))
                this.ltlApartmentGroup.Text = this.additionalInfo.ApartmentGroupName;
            else
                this.ltlApartmentGroup.Visible = false;

            this.pnlWrapper.CssClass = this.pnlWrapper.CssClass + " q-" + (this.QueryState.IsSet ? "set" : "notset");

            this.detailsTable.PopulateControl(group, variant);

            if (blackhouse.WebNemo.Business.Factory.Instance().GetCurrentUser().HasSufficientPermissions(false)) {
                this.pnlActionLinks.Visible = true;
                this.lnkEditApartmentGroup.RecordId = this.group.Id;
                GroupRecord shareType = null;
                if (this.nemoGroup.IsNotNull("wspoldzielony_typ"))
                    shareType = this.nemoGroup.GetRecord("wspoldzielony_typ");
                if (shareType != null && shareType.Exists) {
                    this.lnkEditApartmentGroupShare.GroupSysName = shareType.GetGroup().SystemName;
                    this.lnkEditApartmentGroupShare.RecordId = shareType.RecordId;
                    if (lnkEditApartmentGroupShare.Text.Contains("{0}"))
                        lnkEditApartmentGroupShare.Text = this.lnkEditApartmentGroupShare.Text = String.Format(this.lnkEditApartmentGroupShare.Text, shareType.GetString("nazwa"));
                } else
                    this.lnkEditApartmentGroupShare.Visible = false;
            }
        }

        //[Obsolete]
        //public void PopulateControl(IApartmentGroup group, double pricePerNight, double totalPrice) {
        //    this.SetInstance(group, pricePerNight, totalPrice);

        //    this.imgMainPhoto.ImageUrl = UrlUtils.GetDbImageUrl("Apartment-photo", group.MainPhoto.PhotoName, new System.Drawing.Size(220, 220));
        //    this.imgMainPhoto.AlternateText = group.Name + " - " + group.MainPhoto.Category + " - " + group.MainPhoto.Description;

        //    this.ltlOpinion.Text = this.opinions.GetAverageRatingAsText();
        //    this.ltlOpinionText.Text = this.opinions.GetAverageRatingText();

        //    this.lnkToOpinions.NavigateUrl = UrlUtils.GetOpinionsUrl(this.Factory.GetPreFolderEvaluation(), this.group, null, 0);
        //    this.lnkToOpinions.Text = this.opinions.BasedOnNumberOfOpinions().ToString();
        //    this.lnkToOpinions.ToolTip = this.lnkToOpinions.ToolTip + " - " + this.group.Name;

        //    if (queryState.IsSet && queryState.PeopleCount <= this.group.NumberOfPeople) {
        //        this.pnlPeopleOuter.Width = Unit.Pixel(this.group.NumberOfPeople * 11);
        //        this.pnlPeople.Width = Unit.Pixel(this.queryState.PeopleCount * 11);
        //    }
        //    else {
        //        this.pnlPeople.Width = Unit.Pixel(this.group.NumberOfPeople * 11);
        //    }
        //    string lnkPeople_tooltip = GetGlobalResourceObject("Aparts", "apartmentsPeopleCount").ToString();
        //    if (!string.IsNullOrEmpty(lnkPeople_tooltip) && lnkPeople_tooltip.Contains("{0}"))
        //        this.lnkPeople.ToolTip = string.Format(lnkPeople_tooltip, this.group.NumberOfPeople);

        //    if (this.queryState.IsSet) {
        //        this.lblPriceSet.Visible = true;
        //    }
        //    else {
        //        this.lblPriceNoSet.Visible = true;
        //        this.lblPricePersonNight.Precision = 0;
        //    }
        //    this.lblPricePersonNight.Value = this.price;

        //    if (queryState.IsSet) {
        //        this.pnlPriceAll.Visible = true;
        //        this.lblPriceAll.Value = this.totalPrice;

        //        this.pnlAllNights.Visible = true;
        //        int nights = (int)(this.queryState.DepartureDate.Date - this.queryState.ArrivalDate.Date).TotalDays;
        //        string nightText = nights + " ";
        //        if (nights == 1)
        //            nightText += GetGlobalResourceObject("Aparts", "nights1");
        //        if (nights > 1 && nights <= 4)
        //            nightText += GetGlobalResourceObject("Aparts", "nights24");
        //        if (nights > 4)
        //            nightText += GetGlobalResourceObject("Aparts", "nightsmore");
        //        this.lblNight.Text = nightText;
        //    }

        //    this.ltlApartmentName.Text = this.group.ShortName;

        //    double s = Math.Ceiling(this.group.Stars * 100 / 5);
        //    if (s > 100) s = 100;
        //    if (s < 0) s = 0;
        //    this.pnlStars.Width = Unit.Percentage(s);

        //    this.lblCity.Text = this.group.DefaultLocalization.City;
        //    this.lblDistrict.Text = this.group.DefaultLocalization.District;
        //    this.lblLocation.Text = this.group.DefaultLocalization.LocationName;
        //    this.lnkToPopupMap.Attributes.Add("lat", this.group.DefaultLocalization.Localization.Latitude.ToString(CultureInfo.GetCultureInfo("en-US")));
        //    this.lnkToPopupMap.Attributes.Add("long", this.group.DefaultLocalization.Localization.Longitude.ToString(CultureInfo.GetCultureInfo("en-US")));
        //    this.lnkToPopupMap.NavigateUrl = "/Pages/ShowApartmentMap.aspx?groupId=" + this.group.Id + "&iframe=true&width=960&height=550";
        //    this.lnkToPopupMap.ToolTip = "Lokalizacja apartamentu - " + this.group.ShortName;

        //    IApartmentPhoto photo = this.Factory.GetApartmentPhoto().GetApartmentTypePhotos(this.group.Id).FirstOrDefault(ap => ap.Category.Equals("schemat", StringComparison.CurrentCultureIgnoreCase));
        //    if (photo != null) {
        //        this.lnkToPopupScheme.NavigateUrl = UrlUtils.ApartmentPhotoBigImageUrl(photo.PhotoName);
        //        this.lnkToPopupScheme.ToolTip = group.Name + " - " + photo.Category + " - " + photo.Description;
        //    }
        //    else {
        //        this.lnkToPopupScheme.Visible = false;
        //        this.lnkToPopupMap.CssClass = this.lnkToPopupMap.CssClass.ReplaceEx("-1", "-2");
        //    }



        //    this.lblDescription.Text = GetDescription();
        //    this.lnkFromDescription.NavigateUrl = UrlUtils.GetApartmentTypeUrl(this.Factory.GetPreFolderEvaluation(), this.group, DisplayApartmentTypeView.Default, 0);
        //    this.lnkFromDescription.ToolTip = "Opis " + this.group.Name;

        //    this.ltlSurface.Text = this.group.AreaSize.ToString("F0");

        //    this.rptFields.DataSource = this.group.Fields;
        //    this.rptFields.DataBind();

        //    if (this.queryState.IsSet) {
        //        this.lnkBook.Visible = true;
        //        this.lnkBook.NavigateUrl = NavigationProvider.Instance().GetUrl(ReservationPage.GetInformationPage(this.group.Id));
        //        this.lnkBook.ToolTip = "Zarezerwuj apartament - " + this.group.Name;
        //    }
        //    else {
        //        this.lnkReservationPopup.Visible = true;
        //        this.lnkReservationPopup.Apartment = this.group;
        //    }
        //}

        private void SetInstance(IApartmentGroup group, Variant variant) {
            this.group = group;
            this.nemoGroup = group as NemoApartmentType;
            this.additionalInfo = new AdditionalInformationApartmentType(this.group);
            if (this.nemoGroup == null)
                this.nemoGroup = new NemoApartmentType(this.group.Id);
            this.actors = new CinemaActors(group);
        }

        #endregion

    }

    internal static class ApartExt {

        public static Dictionary<string, Dictionary<ApartmentOptionFieldVariant, string>> variants = new Dictionary<string, Dictionary<ApartmentOptionFieldVariant, string>>();

        public static string OptionFieldToString(this ApartmentOptionFieldVariant variant) {
            string l = CultureInfo.CurrentCulture.Name;

            if (!variants.ContainsKey(l))
                variants[l] = new Dictionary<ApartmentOptionFieldVariant, string>();

            if (!variants[l].ContainsKey(variant))
                variants[l][variant] = HttpContext.GetGlobalResourceObject("Apartments", "optionField" + ((int)variant)).ToString();

            return variants[l][variant];
        }
    }

}